home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OCFSRC.PAK / OCLINK.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  10KB  |  525 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectComponents
  3. // Copyright (c) 1994, 1997 by Borland International, All Rights Reserved
  4. //
  5. // $Revision:   2.9  $
  6. //
  7. // Implementation of TOcLinkView Class
  8. //----------------------------------------------------------------------------
  9. #include <ocf/pch.h>
  10. #if !defined(OCF_OCLINK_H)
  11. # include <ocf/oclink.h>
  12. #endif
  13. #if !defined(OCF_OCAPP_H)
  14. # include <ocf/ocapp.h>
  15. #endif
  16. #if !defined(OCF_OCREMVIEW_H)
  17. # include <ocf/ocremvie.h>
  18. #endif
  19.  
  20. //
  21. //
  22. //
  23. TOcLinkView::TOcLinkView(TOcView* ocView, TRegList* regList, IUnknown* outer)
  24. :
  25.   BSiteI(0), OcView(ocView),
  26.   Origin(0,0),
  27.   Extent(0,0)
  28. {
  29.   PRECONDITION(OcView);
  30.   SetOuter(outer);
  31.   AddRef();    // TUnknown defaults to 0, we need 1
  32.  
  33.   // Create a site for this remote view
  34.   //
  35.   if (SUCCEEDED(OcView->OcApp.BOleComponentCreate(&BSite, (IUnknown*)(IBPart*)this,
  36.       OcView->OcApp.IsOptionSet(amExeModule)? cidBOleSite : cidBOleInProcSite))) {
  37.  
  38.     if (SUCCEEDED(BSite->QueryInterface(IID_IBSite, &(LPVOID)BSiteI)))
  39.       Release();
  40.  
  41.     // Connect the part and the site
  42.     //
  43.     if (BSiteI) {
  44.       const char* progid = regList->Lookup(OcView->OcApp.IsOptionSet(amDebug) ?
  45.                                            "debugprogid" : "progid");
  46.       BSiteI->Init(this, this, OleStr(progid), true);
  47.     }
  48.  
  49.     if (SUCCEEDED(BSite->QueryInterface(IID_IBApplication, &(LPVOID)BAppI)))
  50.       BAppI->Release();     // avoid deadlock
  51.   }
  52. }
  53.  
  54. //
  55. //
  56. //
  57. TOcLinkView::~TOcLinkView()
  58. {
  59.   // Detach the link view
  60.   //
  61.   //Detach();
  62.   if (BSite) {
  63.     BSite->Release();
  64.   }
  65. }
  66.  
  67. //
  68. // Remove this link view from the document
  69. //
  70. int
  71. TOcLinkView::Detach()
  72. {
  73.   return OcView->OcDocument.GetViews().Detach(this);
  74. }
  75.  
  76. //
  77. //
  78. //
  79. HRESULT
  80. TOcLinkView::QueryObject(const IID far& iid, void far* far* iface)
  81. {
  82.   PRECONDITION(iface);
  83.   HRESULT hr;
  84.  
  85.   // interfaces
  86.   //
  87.      SUCCEEDED(hr = IBPart_QueryInterface(this, iid, iface))
  88.   || SUCCEEDED(hr = IBDataProvider_QueryInterface(this, iid, iface))
  89.  
  90.   // helpers
  91.   //
  92.   || (BSite && SUCCEEDED(hr = BSite->QueryInterface(iid, iface)))
  93.   ;
  94.  
  95.   return hr;
  96. }
  97.  
  98. //----------------------------------------------------------------------------
  99. // IBSite pass-thrus
  100.  
  101. //
  102. // Invalidate the site corresponding to this view
  103. //
  104. void
  105. TOcLinkView::Invalidate(TOcInvalidate invalid)
  106. {
  107.   if (BSiteI)
  108.     BSiteI->Invalidate(invalid);
  109. }
  110.  
  111. //
  112. // Disconnect from the client site
  113. //
  114. void
  115. TOcLinkView::Disconnect()
  116. {
  117.   if (BSiteI)
  118.     BSiteI->Disconnect();
  119. }
  120.  
  121. //
  122. // Remember the name of the moniker
  123. //
  124. void
  125. TOcLinkView::SetMoniker(const char far* name)
  126. {
  127.   Moniker  = const_cast<char far*>(name);  // force TString to copy
  128. }
  129.  
  130.  
  131. //----------------------------------------------------------------------------
  132. // IDataNegotiator implementation -- delegate to our owning OcView
  133.  
  134. //
  135. //
  136. //
  137. uint _IFUNC
  138. TOcLinkView::CountFormats()
  139. {
  140.   return OcView->CountFormats();
  141. }
  142.  
  143. //
  144. //
  145. //
  146. HRESULT _IFUNC
  147. TOcLinkView::GetFormat(uint index, TOcFormatInfo far* fmt)
  148. {
  149.   PRECONDITION(fmt);
  150.  
  151.   return OcView->GetFormat(index, fmt);
  152. }
  153.  
  154. //----------------------------------------------------------------------------
  155. // IBDataNegotiator implementation
  156.  
  157. //
  158. // Request native data for pasting into client application.
  159. // This is only called at paste time (not at copy time).
  160. //
  161. HANDLE _IFUNC
  162. TOcLinkView::GetFormatData(TOcFormatInfo far* fmt)
  163. {
  164.   PRECONDITION(fmt);
  165.  
  166.   TOcFormat* format = OcView->FormatList.Find(fmt->Id);
  167.   if (format) {
  168.     TOcFormatData formatData(*format);
  169.     if (OcView->ServerHost->EvOcViewClipData(formatData))
  170.       return formatData.Handle;
  171.   }
  172.  
  173.   return 0;
  174. }
  175.  
  176. //
  177. // Get the initial size and position from the host app into our members
  178. //
  179. void
  180. TOcLinkView::GetLinkRect()
  181. {
  182.   TOcPartSize ps(true, &Moniker);
  183.  
  184.   // Ask the app for initial server extent
  185.   //
  186.   if (!OcView->ServerHost->EvOcViewPartSize(ps)) {
  187.     // An empty rect as default means that the container
  188.     // decides the size for this server
  189.     //
  190.     ps.PartRect.SetNull();
  191.   }
  192.  
  193.   Extent = ps.PartRect.Size();
  194.   Origin = ps.PartRect.TopLeft();
  195. }
  196.  
  197. //
  198. // Render the view in the DC provided. Should be a MetaFile
  199. // Packup all the args & forward message to real view to paint
  200. //
  201. HRESULT _IFUNC
  202. TOcLinkView::Draw(HDC dc, const RECTL far*  pos, const RECTL far* clip,
  203.                   TOcAspect aspect, TOcDraw bd)
  204. {
  205.   PRECONDITION(dc);
  206.  
  207.   // Rely on the bolero shading
  208.   //
  209.   if (bd == drShadingOnly)
  210.     return HR_NOERROR;
  211.  
  212.   TRect p((int)pos->left, (int)pos->top, (int)pos->right, (int)pos->bottom);
  213.   TRect c((int)clip->left, (int)clip->top, (int)clip->right, (int)clip->bottom);
  214.  
  215.   p.SetEmpty();
  216.   ::SetMapMode(dc, MM_ANISOTROPIC);
  217.  
  218.   ::SetWindowExtEx(dc, Extent.cx, Extent.cy, 0);
  219.   ::SetWindowOrgEx(dc, 0, 0, 0);
  220.  
  221.   p.Normalize();
  222.   c.Normalize();
  223.  
  224.   // Find out where the TOleLinkView is
  225.   //
  226.   GetLinkRect();
  227.   *(TPoint*)&p = Origin;
  228.  
  229.   TOcViewPaint vp = { dc, &p, &c, (TOcAspect)aspect, false, &Moniker, 0 };
  230.  
  231.   return HRFailIfZero(OcView->ServerHost->EvOcViewPaint(vp));
  232. }
  233.  
  234. //
  235. // Return the 'size' of the document that this view in on
  236. //
  237. HRESULT _IFUNC
  238. TOcLinkView::GetPartSize(TSize far* size)
  239. {
  240.   *size = Extent;
  241.   return HR_NOERROR;
  242. }
  243.  
  244. //
  245. // Save the document that we are a view on
  246. //
  247. HRESULT _IFUNC
  248. TOcLinkView::Save(IStorage* storage, BOOL sameAsLoad, BOOL remember)
  249. {
  250.   PRECONDITION(storage);
  251.  
  252.   TOcSaveLoad ocSave(storage, ToBool(sameAsLoad), ToBool(remember));
  253.  
  254.   return HRFailIfZero(OcView->ServerHost->EvOcViewSavePart(ocSave));
  255. }
  256.  
  257. #if 0
  258. //
  259. //
  260. //
  261. HRESULT _IFUNC
  262. TOcLinkView::SetFormatData(TOcFormatInfo far* /*fmt*/, HANDLE /*data*/, BOOL /*release*/)
  263. {
  264.   return HR_NOTIMPL;
  265. }
  266. #endif
  267.  
  268. //----------------------------------------------------------------------------
  269. // IBPart implementation
  270.  
  271. //
  272. // Load the associated document and activate the remote view
  273. //
  274. HRESULT _IFUNC
  275. TOcLinkView::Init(IBSite far*, TOcInitInfo far* /*initInfo*/)
  276. {
  277.   return HR_NOERROR;
  278. }
  279.  
  280. //
  281. // Close the remote view window, & if canShutDown is true, try to close the server
  282. // app too
  283. //
  284. HRESULT _IFUNC
  285. TOcLinkView::Close()
  286. {
  287.   OcView->ServerHost->EvOcViewBreakLink(*this);  
  288.   return HR_NOERROR;
  289. }
  290.  
  291. //
  292. // Query to determine if this view can open in place
  293. //
  294. HRESULT _IFUNC
  295. TOcLinkView::CanOpenInPlace()
  296. {
  297.   return HR_FAIL;  // Links never open in place
  298. }
  299.  
  300. //
  301. // Set a new position for our document within its container
  302. //
  303. HRESULT _IFUNC
  304. TOcLinkView::SetPartPos(TRect far* r)
  305. {
  306.   Origin = *(POINT*)&r->left;
  307.   return HR_NOERROR;
  308. }
  309.  
  310. //
  311. //
  312. //
  313. HRESULT _IFUNC
  314. TOcLinkView::SetPartSize(TSize far* size)
  315. {
  316.   Extent = *size;
  317.   return HR_NOERROR;
  318. }
  319.  
  320. //
  321. // Activate this view
  322. //
  323. HRESULT _IFUNC
  324. TOcLinkView::Activate(BOOL /*activate*/)
  325. {
  326.   return HR_NOERROR;
  327. }
  328.  
  329. //
  330. // Show/Hide the server view window
  331. //
  332. HRESULT _IFUNC
  333. TOcLinkView::Show(BOOL /*show*/)
  334. {
  335.   return HR_NOERROR;
  336. }
  337.  
  338. //
  339. // Start or end open editing
  340. // Work with the window Z-order and parenting
  341. //
  342. HRESULT _IFUNC
  343. TOcLinkView::Open(BOOL open)
  344. {
  345.   if (open) {
  346.     TOcRemView* ocRemView = TYPESAFE_DOWNCAST(OcView, TOcRemView);
  347.     if (ocRemView)
  348.       ocRemView->SetOpenEditing();
  349.  
  350.     OcView->ServerHost->EvOcViewAttachWindow(true);
  351.     OcView->BringToFront();
  352.   }
  353.  
  354.   return HR_NOERROR;
  355. }
  356.  
  357. //
  358. // Enumerate the verbs for our document
  359. //
  360. HRESULT _IFUNC
  361. TOcLinkView::EnumVerbs(TOcVerb far*)
  362. {
  363.   return HR_FAIL;  // Not called on BOle parts
  364. }
  365.  
  366. //
  367. // Perform a verb on our document
  368. //
  369. HRESULT _IFUNC
  370. TOcLinkView::DoVerb(uint)
  371. {
  372.   return HR_FAIL;  // Assume that links don't need to do verbs
  373. }
  374.  
  375. //
  376. // Open or close this view as an in-place edit session. If hWndParent is 0, then
  377. // in-place is closing
  378. //
  379. HWND _IFUNC
  380. TOcLinkView::OpenInPlace(HWND /*hWndParent*/)
  381. {
  382.   return 0;
  383. }
  384.  
  385. //
  386. // Insert the server's menus into the shared menubar
  387. //
  388. HRESULT _IFUNC
  389. TOcLinkView::InsertMenus(HMENU /*hMenu*/, TOcMenuWidths far* /*omw*/)
  390. {
  391.   return HR_NOERROR;
  392. }
  393.  
  394. //
  395. // Show or hide the tool windows used by our view
  396. //
  397. HRESULT _IFUNC
  398. TOcLinkView::ShowTools(BOOL /*show*/)
  399. {
  400.   return HR_NOERROR;
  401. }
  402.  
  403. //
  404. // A container window has resized. Perform any necessary adjustment of our
  405. // tools.
  406. //
  407. void _IFUNC
  408. TOcLinkView::FrameResized(const TRect far* /*contFrameR*/, BOOL /*isMainFrame*/)
  409. {
  410. }
  411.  
  412. //
  413. // Let the server provide drag feedback
  414. //
  415. HRESULT _IFUNC
  416. TOcLinkView::DragFeedback(TPoint far* where, BOOL /*nearScroll*/)
  417. {
  418.   TPoint awhere(*where);
  419.   TOcDragDrop dd = { 0, &awhere, 0 };
  420.   return HRFailIfZero(OcView->ServerHost->EvOcViewDrag(dd));
  421. }
  422.  
  423. //
  424. // Optional palette query for
  425. //
  426. HRESULT _IFUNC
  427. TOcLinkView::GetPalette(LOGPALETTE far* far* palette)
  428. {
  429.   PRECONDITION(palette);
  430.  
  431.   return HRFailIfZero(OcView->ServerHost->EvOcViewGetPalette(palette));
  432. }
  433.  
  434. //
  435. //
  436. //
  437. HRESULT _IFUNC
  438. TOcLinkView::SetHost(IBContainer far* /*objContainer*/)
  439. {
  440.   return HR_FAIL;  // Not called on BOle parts.
  441. }
  442.  
  443. //
  444. //
  445. //
  446. HRESULT _IFUNC
  447. TOcLinkView::DoQueryInterface(const IID far& iid, void far* far* iface)
  448. {
  449.   PRECONDITION(iface);
  450.  
  451.   return OcView->QueryInterface(iid, iface);  // Unused on server side
  452. }
  453.  
  454. //
  455. //
  456. //
  457. LPOLESTR _IFUNC
  458. TOcLinkView::GetName(TOcPartName /*name*/)
  459. {
  460.   return 0;  // Not called on BOle parts.
  461. }
  462.  
  463.  
  464. //----------------------------------------------------------------------------
  465. // TOcViewCollection
  466. //
  467.  
  468. //
  469. //
  470. //
  471. TOcViewCollection::TOcViewCollection()
  472. :
  473.   TCVectorImp<TOcLinkView*>(0, 10)
  474. {
  475. }
  476.  
  477. //
  478. //
  479. //
  480. TOcViewCollection::~TOcViewCollection()
  481. {
  482.   Clear();
  483. }
  484.  
  485. //
  486. // Release Views in the collection
  487. //
  488. void
  489. TOcViewCollection::Clear()
  490. {
  491.   for (int i = Count() - 1; i >= 0; i--) {
  492.     TOcLinkView* view = (TOcLinkView*)(*this)[i];
  493.     view->Release();
  494.     Base::Detach(i); 
  495.   }
  496. }
  497.  
  498. //
  499. //
  500. //
  501. int
  502. TOcViewCollection::Detach(TOcLinkView* const& view, int del)
  503. {
  504.   int ret = Base::Detach(Find(view));
  505.   if (ret && del)
  506.     const_cast<TOcLinkView*>(view)->Release();
  507.   return ret;
  508. }
  509.  
  510. //
  511. //
  512. //
  513. TOcLinkView*
  514. TOcViewCollection::Find(TString const moniker) const
  515. {
  516.   for (TOcViewCollectionIter j(*this); j; j++) {
  517.     TOcLinkView* view = (TOcLinkView*)j.Current();
  518.     if (view && strcmp(view->GetMoniker(), moniker) == 0) {
  519.       return view;
  520.     }
  521.   }
  522.  
  523.   return 0;
  524. }
  525.